package cs3343.project.family.UI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.ConfigManager;
import cs3343.project.family.GameController;
import cs3343.project.family.ProfileManager;
import cs3343.project.family.StatisticsController;

/**
 * The Class EndGameFrame.
 */
public class EndGameFrame extends JFrame implements ActionListener{
	
	/** The gc. */
	private GameController gc;
	
	/** The statistics controller. */
	private StatisticsController statisticsController;

	/** The content pane. */
	private JPanel contentPane;
	
	/** The btn graph stat. */
	private JButton btnGraphStat;
	
	/** The btn text stat. */
	private JButton btnTextStat;
	
	/** The btn return. */
	private JButton btnReturn;

	/**
	 * Create the frame.
	 *
	 * @param gc the gc
	 */
	public EndGameFrame(GameController gc) {
		this.gc = gc;
		statisticsController = gc.getStatisticsController();
		
		
		JPanel imgPanel = new JPanel();
		JPanel btnPanel = new JPanel();
		
		setTitle(ConfigManager.getLangString("endGameFrameTitle"));
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 450);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout());
		setContentPane(contentPane);
		
		contentPane.add(imgPanel, BorderLayout.NORTH);
		contentPane.add(btnPanel, BorderLayout.CENTER);		

		
		btnTextStat = new JButton(ConfigManager.getLangString("btnGameStat"));
		btnGraphStat = new JButton(ConfigManager.getLangString("btnGraphStat"));
		btnReturn = new JButton(ConfigManager.getLangString("btnReturn"));
		JLabel endImg = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + "end.jpg"));
		
		imgPanel.add(endImg);
		btnPanel.add(btnTextStat);
		btnPanel.add(btnGraphStat);
		btnPanel.add(btnReturn);
		
		btnTextStat.addActionListener(this);
		btnGraphStat.addActionListener(this);
		btnReturn.addActionListener(this);

		gc.getUiController().frameCentering(this);
		
		statisticsController.getGameResult();
		statisticsController.loadGlobalGameStat();		
		statisticsController.saveGlobalGameStat();
		
		for (int i = 0; i < gc.getCharacterController().getNumOfPlayer(); i++)
			ProfileManager.updateProfile(gc.getCharacterController().getPlayer(i));
		
		statisticsController.saveLastGameStat();
		
		String folderPath = ConfigManager.generateGameStatisticsFolderName();		
		statisticsController.generateGameLogFile(folderPath);
		statisticsController.generatePlayerLogFile(folderPath);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnTextStat) {
			gc.getUiController().showGlobalGameStatFrame();
		}
		else if (e.getSource() == btnGraphStat) {
			gc.getUiController().showGameStatFrame();
		}
		else if (e.getSource() == btnReturn) {
			gc.getUiController().showGameSettingFrame();
			this.dispose();
		}
	}
}
